package com.course.web.biz;

import com.course.domain.model.*;
import com.course.domain.service.TaskService;
import com.course.web.model.*;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by Justin on 2017/6/11.
 */
@Component
public class WebTaskBiz {

    private final TaskService taskService;

    @Autowired
    public WebTaskBiz(TaskService taskService) {
        this.taskService = taskService;
    }

    public TaskAudioVM createAudio(TaskAudioVM vm) {
        TaskAudio taskAudio = taskService.createAudio(toAudio(vm));
        vm.setId(taskAudio.getId());
        return vm;
    }

    public TaskAudioVM updateAudio(TaskAudioVM vm) {
        taskService.updateAudio(toAudio(vm));
        return vm;
    }

    public TaskVideoVM createVideo(TaskVideoVM vm) {
        TaskVideo taskVideo = taskService.createVideo(toVideo(vm));
        vm.setId(taskVideo.getId());
        return vm;
    }

    public TaskVideoVM updateVideo(TaskVideoVM vm) {
        taskService.updateVideo(toVideo(vm));
        return vm;
    }

    public TaskStoryVM createStory(TaskStoryVM vm) {
        TaskStory taskStory = taskService.createStory(toStory(vm));
        vm.setId(taskStory.getId());
        return vm;
    }

    public TaskStoryVM updateStory(TaskStoryVM vm) {
        taskService.updateStory(toStory(vm));
        return vm;
    }

    public TaskClassVM createClass(TaskClassVM vm) {
        TaskClass taskClass = taskService.createClass(toClass(vm));
        vm.setId(taskClass.getId());
        return vm;
    }

    public TaskClassVM updateClass(TaskClassVM vm) {
        taskService.updateClass(toClass(vm));
        return vm;
    }

    public TaskEventVM createEvent(TaskEventVM vm) {
        TaskEvent taskEvent = taskService.createEvent(toEvent(vm));
        vm.setId(taskEvent.getId());
        return vm;
    }

    public TaskEventVM updateEvent(TaskEventVM vm) {
        taskService.updateEvent(toEvent(vm));
        return vm;
    }

    public TaskGroupVM createGroup(TaskGroupVM vm) {
        TaskGroup taskGroup = taskService.createGroup(toGroup(vm));
        vm.setId(taskGroup.getId());
        return vm;
    }

    public TaskGroupVM updateGroup(TaskGroupVM vm) {
        taskService.updateGroup(toGroup(vm));
        return vm;
    }

    public TaskGroupVM deleteGroup(TaskGroupVM vm) {
        if (vm.getId() > 0) {
            taskService.removeGroup(vm.getId());
        }
        return vm;
    }

    public TaskLearnVM createLearn(TaskLearnVM vm) {
        TaskLearn taskLearn = taskService.createLearn(toLearn(vm));
        vm.setId(taskLearn.getId());
        return vm;
    }

    public List<TaskAudioVM> findAudio(int courseId) {
        return Lists.transform(taskService.findAudio(courseId), this::transform);
    }

    public List<TaskVideoVM> findVideo(int courseId) {
        return Lists.transform(taskService.findVideo(courseId), this::transform);
    }

    public List<TaskStoryVM> findStory(int courseId) {
        return Lists.transform(taskService.findStory(courseId), this::transform);
    }

    public List<TaskClassVM> findClass(int courseId) {
        return Lists.transform(taskService.findClass(courseId), this::transform);
    }

    public List<TaskEventVM> findEvent(int courseId) {
        return Lists.transform(taskService.findEvent(courseId), this::transform);
    }

    public List<TaskGroupVM> findGroup(int courseId) {
        return Lists.transform(taskService.findGroup(courseId), this::transform);
    }

    private TaskAudioVM transform(TaskAudio taskAudio) {
        TaskAudioVM vm = new TaskAudioVM(taskAudio.getCourseId());
        vm.setId(taskAudio.getId());
        vm.setTitle(taskAudio.getTitle());
        vm.setTimes(taskAudio.getTimes());
        vm.setSpeed(taskAudio.getSpeed());
        vm.setDetail(taskAudio.getDetail());
        vm.setAudioLink(taskAudio.getAudioLink());
        vm.setAlbumLink(taskAudio.getAlbumLink());
        return vm;
    }

    private TaskAudio toAudio(TaskAudioVM vm) {
        TaskAudio taskAudio = new TaskAudio();
        taskAudio.setId(vm.getId());
        taskAudio.setCourseId(vm.getCourseId());
        taskAudio.setTitle(vm.getTitle());
        taskAudio.setTimes(vm.getTimes());
        taskAudio.setSpeed(vm.getSpeed());
        taskAudio.setDetail(vm.getDetail());
        taskAudio.setAudioLink(vm.getAudioLink());
        taskAudio.setAlbumLink(vm.getAlbumLink());
        taskAudio.setStatus(1);
        return taskAudio;
    }

    private TaskVideoVM transform(TaskVideo taskVideo) {
        TaskVideoVM vm = new TaskVideoVM(taskVideo.getCourseId());
        vm.setId(taskVideo.getId());
        vm.setTitle(taskVideo.getTitle());
        vm.setTimes(taskVideo.getTimes());
        vm.setDetail(taskVideo.getDetail());
        vm.setVideoLink(taskVideo.getVideoLink());
        vm.setAlbumLink(taskVideo.getAlbumLink());
        return vm;
    }

    private TaskVideo toVideo(TaskVideoVM vm) {
        TaskVideo taskVideo = new TaskVideo();
        taskVideo.setId(vm.getId());
        taskVideo.setCourseId(vm.getCourseId());
        taskVideo.setTitle(vm.getTitle());
        taskVideo.setTimes(vm.getTimes());
        taskVideo.setDetail(vm.getDetail());
        taskVideo.setVideoLink(vm.getVideoLink());
        taskVideo.setAlbumLink(vm.getAlbumLink());
        taskVideo.setStatus(1);
        return taskVideo;
    }

    private TaskStoryVM transform(TaskStory taskStory) {
        TaskStoryVM vm = new TaskStoryVM(taskStory.getCourseId());
        vm.setId(taskStory.getId());
        vm.setTitle(taskStory.getTitle());
        vm.setTimes(taskStory.getTimes());
        vm.setSpeed(taskStory.getSpeed());
        vm.setBookState(taskStory.getBookState());
        vm.setDetail(taskStory.getDetail());
        vm.setStoryLink(taskStory.getStoryLink());
        vm.setAlbumLink(taskStory.getAlbumLink());
        return vm;
    }

    private TaskStory toStory(TaskStoryVM vm) {
        TaskStory taskStory = new TaskStory();
        taskStory.setId(vm.getId());
        taskStory.setCourseId(vm.getCourseId());
        taskStory.setTitle(vm.getTitle());
        taskStory.setTimes(vm.getTimes());
        taskStory.setSpeed(vm.getSpeed());
        taskStory.setBookState(vm.getBookState());
        taskStory.setDetail(vm.getDetail());
        taskStory.setStoryLink(vm.getStoryLink());
        taskStory.setAlbumLink(vm.getAlbumLink());
        taskStory.setStatus(1);
        return taskStory;
    }

    private TaskClassVM transform(TaskClass taskClass) {
        TaskClassVM vm = new TaskClassVM(taskClass.getCourseId());
        vm.setId(taskClass.getId());
        vm.setTitle(taskClass.getTitle());
        vm.setClassType(taskClass.getType());
        vm.setMinutes(taskClass.getMinutes());
        vm.setDetail(taskClass.getDetail());
        vm.setPptLink(taskClass.getPptLink());
        return vm;
    }

    private TaskClass toClass(TaskClassVM vm) {
        TaskClass taskClass = new TaskClass();
        taskClass.setId(vm.getId());
        taskClass.setCourseId(vm.getCourseId());
        taskClass.setTitle(vm.getTitle());
        taskClass.setType(vm.getClassType());
        taskClass.setMinutes(vm.getMinutes());
        taskClass.setDetail(vm.getDetail());
        taskClass.setPptLink(vm.getPptLink());
        taskClass.setStatus(1);
        return taskClass;
    }

    private TaskEventVM transform(TaskEvent taskEvent) {
        TaskEventVM vm = new TaskEventVM(taskEvent.getCourseId());
        vm.setId(taskEvent.getId());
        vm.setTitle(taskEvent.getTitle());
        vm.setMinutes(taskEvent.getMinutes());
        vm.setDetail(taskEvent.getDetail());
        return vm;
    }

    private TaskEvent toEvent(TaskEventVM vm) {
        TaskEvent taskEvent = new TaskEvent();
        taskEvent.setId(vm.getId());
        taskEvent.setCourseId(vm.getCourseId());
        taskEvent.setTitle(vm.getTitle());
        taskEvent.setMinutes(vm.getMinutes());
        taskEvent.setDetail(vm.getDetail());
        taskEvent.setStatus(1);
        return taskEvent;
    }

    private TaskGroupVM transform(TaskGroup taskGroup) {
        TaskGroupVM vm = new TaskGroupVM(taskGroup.getCourseId());
        vm.setId(taskGroup.getId());
        vm.setIdx(taskGroup.getIdx());
        vm.setTaskId(taskGroup.getTaskId());
        vm.setItemId(taskGroup.getItemId());
        vm.setType(taskGroup.getType());
        return vm;
    }

    private TaskGroup toGroup(TaskGroupVM vm) {
        TaskGroup taskGroup = new TaskGroup();
        taskGroup.setId(vm.getId());
        taskGroup.setCourseId(vm.getCourseId());
        taskGroup.setIdx(vm.getIdx());
        taskGroup.setTaskId(vm.getTaskId());
        taskGroup.setItemId(vm.getItemId());
        taskGroup.setType(vm.getType());
        taskGroup.setStatus(1);
        return taskGroup;
    }

    private TaskLearn toLearn(TaskLearnVM vm) {
        TaskLearn taskLearn = new TaskLearn();
        taskLearn.setId(vm.getId());
        taskLearn.setCourseId(vm.getCourseId());
        taskLearn.setTitle("");
        taskLearn.setStatus(1);
        return taskLearn;
    }
}
